package com.wei.duowan.bbs.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wei.duowan.bbs.App;
import com.wei.duowan.bbs.R;
import com.wei.duowan.bbs.model.ShareChannelBean;
import com.wei.duowan.bbs.ui.adapter.BaseRecyclerViewAdapter;
import com.wei.duowan.bbs.ui.adapter.BaseViewHolder;
import com.wei.duowan.bbs.utils.StringUtils;

import java.util.List;

/**
 * 作者：赵若位
 * 时间：2019/9/19 10:37
 * 联系方式：1070138445
 * 功能：
 */
public class ShareUtils
{
    private PopupWindow mWindow;
    private Context mContext;
    private int mLayoutID;
    private String mShareFileName;
    private int mAnimationStyle;


    private ShareUtils(@Nullable Context context)
    {
        if (context == null)
        {
            throw new NullPointerException("The Context of SharePopuWindow is empty!");
        }
        this.mContext = context;
    }

    /*初始化相关操作*/
    private void init()
    {
        View layout = LayoutInflater.from(mContext).inflate(mLayoutID, null);
        final AppCompatActivity activity = (AppCompatActivity) layout.getContext();

        mWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        mWindow.setBackgroundDrawable(new ColorDrawable());
        mWindow.setOutsideTouchable(true);

        setBackground(activity);

        //dismiss时恢复原样
        mWindow.setOnDismissListener(() ->
        {
            WindowManager.LayoutParams params = activity.getWindow().getAttributes();
            params.alpha = 1f;
            activity.getWindow().setAttributes(params);
        });
        mWindow.setAnimationStyle(R.style.PopuWindowDefaultAnimation);
        //
        RecyclerView recyclerView = layout.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(App.getContext(), 5));
        BaseRecyclerViewAdapter adapter = new BaseRecyclerViewAdapter<ShareChannelBean>(activity, R.layout.layout_share)
        {
            @Override
            protected void convert(int position, @Nullable BaseViewHolder holder, @Nullable final ShareChannelBean data)
            {
                holder.setImageResourceWithField(R.id.img_share, data.getIcon())
                        .setText(R.id.tv_share, data.getName())
                        .itemView.setOnClickListener(view ->
                {
                    Toast.makeText(mActivity, data.getName(), Toast.LENGTH_SHORT).show();
                    dismiss();
                });
            }
        };
        adapter.bindRecyclerView(recyclerView);
        adapter.setNewData(getShareData());
    }

    /*设置灰色的遮光罩*/
    private void setBackground(@Nullable Activity activity)
    {
        Window window = activity.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.alpha = 0.7f;//调节透明度
        window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        window.setAttributes(params);
    }

    private List<ShareChannelBean> getShareData()
    {
        if (TextUtils.isEmpty(mShareFileName))
        {
            mShareFileName = "头条分享配置文件.json";
        }
        return new Gson().fromJson(StringUtils.getAssetsJson(mShareFileName), new TypeToken<List<ShareChannelBean>>()
        {
        }.getType());
    }

    /*Show*/
    public void show(@Nullable View layout)
    {
        if (mWindow != null && layout != null)
        {
            AppCompatActivity activity = (AppCompatActivity) layout.getContext();
            setBackground(activity);
            mWindow.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
        }
    }


    /*Dismiss*/
    public void dismiss()
    {
        if (mWindow != null && mWindow.isShowing())
        {
            mWindow.dismiss();
        }
    }

    public static class Builder
    {
        private ShareUtils mUtils;

        public Builder(@Nullable Context context)
        {
            mUtils = new ShareUtils(context);
        }

        public Builder setContentView(int layoutID)
        {
            mUtils.mLayoutID = layoutID;
            return this;
        }

        public Builder setShareFileName(@Nullable String fileName)
        {
            mUtils.mShareFileName = fileName;
            return this;
        }

        public Builder setAnimationStyle(int style)
        {
            mUtils.mAnimationStyle = style;
            return this;
        }


        public ShareUtils build()
        {
            mUtils.init();
            return mUtils;
        }

    }
}
